"""
下载工作线程模块
负责在后台线程中执行色块生成任务
"""

from PySide6.QtCore import QObject, Signal


class DownloadWorker(QObject):
    """下载工作线程"""
    
    finished = Signal(bool, str)
    
    def __init__(self, generator, hex_color, width, height, filepath, format_name):
        """
        初始化下载工作线程
        
        Args:
            generator: 色块生成器实例
            hex_color (str): 16进制颜色值
            width (int): 图片宽度
            height (int): 图片高度
            filepath (str): 保存路径
            format_name (str): 图片格式
        """
        super().__init__()
        self.generator = generator
        self.hex_color = hex_color
        self.width = width
        self.height = height
        self.filepath = filepath
        self.format_name = format_name
    
    def run(self):
        """执行下载任务"""
        try:
            success = self.generator.generate_and_save(
                self.hex_color, self.width, self.height,
                self.filepath, self.format_name
            )
            self.finished.emit(success, self.filepath)
        except Exception as e:
            self.finished.emit(False, str(e))